package com.snailhd.listener;

import com.snailhd.entity.StockRealTime;
import com.snailhd.mongodb.MongodbDocEnums;
import com.snailhd.redis.RedisOperations;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.CompoundIndexDefinition;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 保存实时数据到mongodb
 * @author hd
 * @date 2019-11-24 18:38
 */

@Slf4j
@Component
public class StockRealTimeSaveMongodbListener implements Listener<List<StockRealTime>> {

    @Resource
    MongoTemplate mongoTemplate;

    @Resource
    RedisOperations redisOperations;

    @Override
    public void monitor(List<StockRealTime> stockRealTimes) {
        if (stockRealTimes.size()>0){
            String docName = MongodbDocEnums.STOCK_REAL_TIME + "_" + stockRealTimes.get(0).getDate();
            BulkOperations ops = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,docName);
            for (StockRealTime it:stockRealTimes ){
                setOnInsert(ops,it);
            }
            createIndex(docName);
            ops.execute();
        }
    }

    private void setOnInsert(BulkOperations ops,StockRealTime it){
        ops.upsert(new Query(Criteria.where("code").is(it.getCode()).and("time").is(it.getTime())),
                new Update().setOnInsert("code",it.getCode())
                        .setOnInsert("time",it.getTime())
                        .setOnInsert("symbol",it.getSymbol())
                        .setOnInsert("buyBets",it.getBuyBets())
                        .setOnInsert("sellBets",it.getSellBets())
                        .setOnInsert("date",it.getDate())
                        .setOnInsert("amount",it.getAmount())
                        .setOnInsert("changepercent",it.getChangepercent())
                        .setOnInsert("high",it.getHigh())
                        .setOnInsert("low",it.getLow())
                        .setOnInsert("open",it.getOpen())
                        .setOnInsert("pricechange",it.getPricechange())
                        .setOnInsert("settlement",it.getSettlement())
                        .setOnInsert("trade",it.getTrade())
                        .setOnInsert("volume",it.getVolume()));
    }


    private void createIndex(String collectionName){
        if(!redisOperations.existStockRealTimeIndex(collectionName)){
            IndexOperations indexOperations = mongoTemplate.indexOps(collectionName);
            Document doc = new Document("code",1);
            doc.put("time", 1);
            Index index = new CompoundIndexDefinition(doc).background().unique();
            indexOperations.ensureIndex(index);
            redisOperations.saveStockRealTimeIndex(collectionName);
        }
    }
}
